package cn.lrwjz.androidguide.ui;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.switchmaterial.SwitchMaterial;

import cn.lrwjz.androidguide.R;

public class SwitchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Switch 示例");
        }

        // 设置基础开关的监听
        SwitchMaterial basicSwitch = findViewById(R.id.basicSwitch);
        basicSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String state = isChecked ? "开启" : "关闭";
            Toast.makeText(this, "基础开关：" + state, Toast.LENGTH_SHORT).show();
        });

        // 设置带图标开关
        SwitchMaterial iconSwitch = findViewById(R.id.iconSwitch);
        
        // 设置开关颜色
        int purple500 = ContextCompat.getColor(this, R.color.purple_500);
        int purple200 = ContextCompat.getColor(this, R.color.purple_200);
        iconSwitch.setThumbTintList(ColorStateList.valueOf(purple500));
        iconSwitch.setTrackTintList(ColorStateList.valueOf(purple200));
        
        // 设置图标
        Drawable icon = ContextCompat.getDrawable(this, android.R.drawable.ic_media_play);
        if (icon != null) {
            icon.setBounds(0, 0, 48, 48);
            iconSwitch.setCompoundDrawables(icon, null, null, null);
        }
        
        // 设置监听
        iconSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String state = isChecked ? "开启" : "关闭";
            Toast.makeText(this, "带图标开关：" + state, Toast.LENGTH_SHORT).show();
        });

        // 设置禁用状态开关的初始状态
        SwitchMaterial disabledSwitch = findViewById(R.id.disabledSwitch);
        disabledSwitch.setChecked(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 